package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.StoreActivityQueueListDto;
import com.frejoys.common.dto.admin.StoreIdAndPageDto;
import com.frejoys.common.dto.admin.StoreIdAndStoreActivityIdDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.OrderSnDto;
import com.frejoys.common.enums.EAccountType;
import com.frejoys.common.enums.EStoreActivityQueueStatus;
import com.frejoys.common.enums.EUserWalletLogType;
import com.frejoys.common.enums.EVerify;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.StoreActivityQueueListVo;
import com.frejoys.common.vo.store.StoreActivityQueueVo;
import com.frejoys.dao.entity.Order;
import com.frejoys.dao.entity.StoreActivityQueue;
import com.frejoys.service.OrderService;
import com.frejoys.service.RebateService;
import com.frejoys.service.StoreActivityQueueService;
import com.frejoys.service.UserWalletLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "店铺活动队列", description = "店铺活动队列")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminStoreActivityQueueController {

    private final StoreActivityQueueService storeActivityQueueService;

    private final RedissonClient redissonClient;

    private final RebateService rebateService;

    private final UserWalletLogService userWalletLogService;

    private final OrderService orderService;
    /**
     * 查询用户参与店铺活动队列
     * @param dto
     * @return
     */
    @PostMapping("/activityQueue/listInfo")
    @Operation(summary = "查询用户参与店铺活动队列")
    public Result<List<StoreActivityQueueVo>> listInfo(@RequestBody @Validated StoreActivityQueueListDto dto) {
        return Result.success(storeActivityQueueService.adminListInfo(dto));
    }


    @PostMapping("/activityQueue/activityQueueListInfo")
    @Operation(summary = "查询店铺铺活动队列")
    public Result<IPage<StoreActivityQueueListVo>> activityQueueListInfo(@RequestBody @Validated StoreIdAndStoreActivityIdDto dto) {
        return Result.success(storeActivityQueueService.activityQueueListInfo(dto));
    }

    @Operation(summary = "管理员手动返利")
    @PostMapping("/activityQueue/orderRebate")
    public Result orderRebate(@RequestBody @Validated OrderSnDto dto){

        RLock lock = redissonClient.getLock(CacheKeyUtil.taskRebateQueueLockKey());

        if (lock.tryLock()) {
            try {

                //获取活动队列
                StoreActivityQueue storeActivityQueue = storeActivityQueueService.getStoreActivityQueueInfo(dto.getOrderSn());

                //校验活动队列
                if (storeActivityQueueService.verifyActivityQueue(storeActivityQueue)) {
                    // 手动处理活动队列
                    rebateService.activityQueueAdmin(storeActivityQueue);
                }

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }

    @Operation(summary = "作废返利")
    @PostMapping("/activityQueue/finishOrderRebate")
    public Result finishOrderRebate(@RequestBody @Validated OrderSnDto dto){

        RLock lock = redissonClient.getLock(CacheKeyUtil.upActivityQueueLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                //获取活动队列
                StoreActivityQueue storeActivityQueue = storeActivityQueueService.getStoreActivityQueueInfo(dto.getOrderSn());

                //校验活动队列
                storeActivityQueueService.verifyActivityQueue(storeActivityQueue);

                //作废返利
                storeActivityQueueService.finishOrderRebate(storeActivityQueue.getOrderSn(),storeActivityQueue.getUserId());

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }
}
